package org.example.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        // 获取字节码文件对象
        Class<?> aClass = Class.forName("org.example.pojo.User");
        // 获取构造方法
        Constructor<?>[] constructors = aClass.getConstructors(); //所以公共构造方法
        Constructor<?>[] declaredConstructor = aClass.getDeclaredConstructors(); //所有定义了的构造方法
        for (Constructor c : declaredConstructor) {
            System.out.println(c);
        }

        // 获取单个构造方法
        Constructor<?> constructor = aClass.getDeclaredConstructor(String.class, String.class);
        System.out.println(constructor);

        System.out.println("#########");

        Method[] methods = aClass.getMethods();
        for (Method m : methods) {
            System.out.println(m);
        }
        Method method = aClass.getMethod("show");
        System.out.println("method:"+method);
        Object o = aClass.newInstance();
        method.invoke(o);
    }
}
